package com.example.demo.item.service.impl;

import com.example.demo.item.dao.ItemMapper;
import com.example.demo.item.entity.Item;
import com.example.demo.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ItemServiceImpl implements ItemService {


    @Autowired
    private ItemMapper itemMapper;

    /**
     * 添加多个用户
     * @param item
     * @return
     */
    @Override
    public List<Item> findList(Item item) {
        List<Item> list = itemMapper.findList(item);
        return list;
    }

    @Override
    public Item findOne(String id) {

        Item item = itemMapper.selectByPrimaryKey(id);
        return item;
    }

    @Override
    public void remove(String id) {

        itemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Item item) {

        itemMapper.insert(item);
    }

    @Override
    public void update(Item item) {

        itemMapper.updateByPrimaryKey(item);
    }

    /**
     * 插入十万条数据
     */
    @Override
    public void insert() {

        Date date = new Date();
        for (int i= 0;i<100000;i++){
            Item item = new Item();
            String uuid = UUID.randomUUID().toString().replace("-","");
            item.setId(uuid);
            item.setName("商品:"+ i);
            item.setUrl("http://baidu" + i+ ".com");
            item.setCount(i+100.0);
            item.setDiscription("商品是真的好"+i);
            item.setCreateBy(null);
            item.setUpdateTime(date);
            item.setCreateBy("1");
            item.setDeleted(0);
            itemMapper.insert(item);

        }
        method();

    }

    /**
     * 批量插入
     */
    private void method(){

        List<Item> itemList = new ArrayList<>();
        Date date = new Date();
        for (int i=0;i<100000;i++){
            Item item = new Item();
            String uuid = UUID.randomUUID().toString().replace("-","");
            item.setId(uuid);
            item.setName("商品:"+ i);
            item.setUrl("http://baidu" + i+ ".com");
            item.setCount(i+100.0);
            item.setDiscription("商品是真的好"+i);
            item.setCreateBy(null);
            item.setUpdateTime(date);
            item.setCreateBy("1");
            item.setDeleted(0);
            itemList.add(item);
        }
        itemMapper.insertBatch(itemList);
    }


}
